<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * caidan
 */
namespace app\home\model;

use app\home\model\Model;

class Menu extends Model{

	private $menu = 'web_menu';

    /**
     * 菜單列表
     * @param array $menuList
     * @param array $where
     */
    public function menuList($where='', $lang='default')
    {
        $menuList = Model::name($this->menu)
            ->field('*')
            ->where('pid=0')
            ->where($where)
            ->order('orderid asc')
            ->select()
            ->toArray();
        foreach ($menuList as $key => $value) {
            if(!empty($value['titles'])){
                $menuList[$key]['title']=isset(json_decode($value['titles'],true)[$lang])?json_decode($value['titles'],true)[$lang]:$value['title'];
            }
            unset($menuList[$key]['titles']);
            $menuList[$key]['submenu']=Model::name($this->menu)
                ->field('*')
                ->where('pid='.$value['id'])
                ->where($where)
                ->order('orderid asc')
                ->select()
                ->toArray();
            foreach ($menuList[$key]['submenu'] as $k => $val) {
                if(!empty($value['titles'])){
                    $menuList[$key]['submenu'][$k]['title']=isset(json_decode($val['titles'],true)[$lang])?json_decode($val['titles'],true)[$lang]:$val['title'];  
                }
                unset($menuList[$key]['submenu'][$k]['titles']);
            }
        }
        return $menuList;
    }


}
